package CelestialWorkflow;

import CelestialDataAccess.*;
import javax.sql.rowset.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class ShoppingCartControl {

    public static final int NULL_EMAIL = 1;         // no email supplied
    public static final int NULL_PWD = 2;           // no password supplied
    public static final int EMAIL_NT_REG = 3;     // no records found
    public static final int WRONG_PWD = 4;          // password mismatch
    public static final int INVALID_EMAIL = 5;      // email syntax is worng
    public static final int INACTIVE_ACC = 6;       // inactive account
    public static final int SYS_ERR = 7;            // system error, see message
    private int m_iError = 0;
    private String m_strErrorMessage = "";

    /*************************************************************************/
    /* get/set methods */
    public int getError() {
        return m_iError;
    }

    public String getErrorMessage() {
        return m_strErrorMessage;
    }

    /*************************************************************************/
    /* constructors */
    public ShoppingCartControl() {
    }

    /*************************************************************************/
    /* client methods */
    public Customer getCustomerById(int customerID) {

        /* clear error message */
        m_iError = 0;
        m_strErrorMessage = "";

        /* begin validate against database*/
        try {
            /* return customer information*/
            CustomersDataAccess daUsers = new CustomersDataAccess();
            Customer usr = daUsers.getCustomerByID(customerID);
            return usr;
        } catch (DataAccessException ex1) {
            m_iError = SYS_ERR;
            m_strErrorMessage = ex1.getMessage();
            return null;
        }
    }

    /*************************************************************************/
    public String newOrder(String productsCookie, String customerData, int customerID) {
        m_iError = 0;
        m_strErrorMessage = "";

        Order newOrder = new Order();
        newOrder.setCustomerID(customerID);
        newOrder.setOrderDate(new Date());
        newOrder.setStatus(0);
        newOrder.setEmployeeID(-1);
        newOrder.setDispatchDate(null);
        newOrder.setRemarks(null);
        String[] products;
        products = productsCookie.split("/");
        ProductsDataAccess daProducts = new ProductsDataAccess();
        Product orderProduct;
        String[] productData;

        try {
            for (int i = 0; i < products.length; i++) {
                productData = products[i].split(":");
                orderProduct = daProducts.getProductByID(productData[0]);
                newOrder.addItem(orderProduct, Integer.parseInt(productData[2]));
            }
        } catch (DataAccessException ex1) {
            m_iError = SYS_ERR;
            m_strErrorMessage = ex1.getMessage();
            return "-1";
        }

        String[] arrayCustomerData = customerData.split("///");
        Address addrShipping = new Address();
        addrShipping.Country = arrayCustomerData[14];
        addrShipping.City = arrayCustomerData[15];
        addrShipping.State = arrayCustomerData[16];
        addrShipping.AddressLine1 = arrayCustomerData[17];
        addrShipping.AddressLine2 = arrayCustomerData[18];
        addrShipping.PostalCode = arrayCustomerData[19];
        newOrder.setShippingAddress(addrShipping);

        Address addrBilling = new Address();
        addrBilling.Country = arrayCustomerData[8];
        addrBilling.City = arrayCustomerData[9];
        addrBilling.State = arrayCustomerData[10];
        addrBilling.AddressLine1 = arrayCustomerData[11];
        addrBilling.AddressLine2 = arrayCustomerData[12];
        addrBilling.PostalCode = arrayCustomerData[13];
        newOrder.setBillingAddress(addrBilling);

        OrdersDataAccess daOrders = new OrdersDataAccess();
        try {
            daOrders.insertOrder(newOrder);
            return newOrder.getOrderID().toString();
        } catch (DataAccessException ex1) {
            m_iError = SYS_ERR;
            m_strErrorMessage = ex1.getMessage();
            return "-1";
        }
    }

    public void suggestProducts(String productsCookie, int customerID, List<Product> lst) {
        m_iError = 0;
        m_strErrorMessage = "";
        String[] products = productsCookie.split(",");
        List<Product> tempList = new ArrayList<Product>();
        List<Product> fetchedList = new ArrayList<Product>();
        ProductsDataAccess daProducts = new ProductsDataAccess();

        try {
            for (int i = 0; i < products.length; i++) {
                daProducts.suggestProductsByProductID(customerID, products[0], fetchedList);
                for (int j = 0; j < 3; j++) {
                    if (fetchedList.get(j) != null) {
                        tempList.add(fetchedList.get(j));
                    }
                }
            }
        } catch (DataAccessException ex1) {
            m_iError = SYS_ERR;
            m_strErrorMessage = ex1.getMessage();
        }
        
        int random = 0, first = -1, second = -1;
        if (tempList == null) {
            //do nothing
        }
        else if (tempList.size() <= 3) {
            lst = tempList;
        }
        else {
            for (int j = 0; j < 3; j++) {
                do {
                    random = (int) Math.random() * tempList.size();
                } while (random == first || random == second);

                if (first == -1) {
                    first = random;
                } else {
                    second = random;
                }
                lst.add(tempList.get(random));
            }
        }
    }
}
